package neu.soft.util;

import cn.hutool.extra.pinyin.engine.pinyin4j.Pinyin4jEngine;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

/**
 * @author 李博
 * 商品代码生成工具包，内含自定义的汉字拼音转换方法
 */
public class ChineseCharacterUtils {

    private static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

    public static char[] getNameFormat(String targetStr,int len){

        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        Pinyin4jEngine engine = new Pinyin4jEngine(format);
        char[] targetPart = targetStr.toCharArray();
        char[] headChars = new char[len];
        for (int i = 0;i < len; i++){
            String pinyin = engine.getPinyin(targetPart[i]);
            headChars[i] = pinyin.charAt(0);
        }
        return headChars;
    }

    public static char[] getPinYinForGood(String targetStr){
        int len = targetStr.length();
        int split = len/2;
        if (split>6&&split<=9){
            split = split/2;
        }else if (split>9){
            split = split/3;
        }
        return getNameFormat(targetStr,split);
    }




}